package com.blb.java08springboot.controller;

import com.blb.java08springboot.entity.Student;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class StudentController {

    List<Student> students = new ArrayList<>();
    {
        students.add(new Student(1L,"001","张三",20,"武汉","男",1,"https://img1.baidu.com/it/u=1960110688,1786190632&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"));
        students.add(new Student(2L,"002","李四",20,"武汉","男",1,"xx.jpg"));
        students.add(new Student(3L,"003","王五",20,"武汉","男",1,"xx.jpg"));
        students.add(new Student(4L,"004","赵六",20,"武汉","男",1,"xx.jpg"));
    }

    @RequestMapping("students")
    public String findAll(Model model){
        model.addAttribute("msg","Hello Thymeleaf");
        model.addAttribute("age",20);
        model.addAttribute("students",students);
        return "students";
    }

    @RequestMapping("findById")
    public String findById(Long id, Model model){
        Student stu = students.stream().filter(student -> student.getStuId() == id)
                .collect(Collectors.toList()).get(0);
        model.addAttribute("student",stu);
        return "student";
    }

    @RequestMapping("update")
    public String updateById(Student student){
        Student stu2 = students.stream().filter(stu -> stu.getStuId() == student.getStuId())
                .collect(Collectors.toList()).get(0);
        BeanUtils.copyProperties(student,stu2);
        return "forward:students";
    }
}
